﻿#include "UDP_Test.h"

#include <qdebug.h>

#include "Codec_NBIoT.h"
#include "LogSaver.h"

#include "MacroDefinition.h"

UDP_Test::UDP_Test()
{
  /*
  数据类型1字节，
  消息ID2字节，
  设备ID4字节，
  时间戳ID2字节
  载荷
  */
  logSaver = LogSaver::getLogSaver();
  connect(&timer_register, &QTimer::timeout, this, &UDP_Test::slot_register);
  connect(&timer_life,&QTimer::timeout,[this](){logSaver->saveLog("alive");});
  timer_life.start(5*1000);
}

UDP_Test::~UDP_Test()
{

}

void UDP_Test::startTest()
{
  timer_register.start(timer_interval_checkConnection_NBIOT);
}

QByteArray UDP_Test::makeRegisterData(ushort msgId)
{
  QByteArray data;
  data.append(0x04);
  data.append((char*)&msgId,sizeof (msgId));
  data.append((char*)&deviceId,sizeof (deviceId));
  data.append(2,0);
  data.append(1);
  return data;
}

void UDP_Test::slot_register()
{
  if (!codec_NBIoT->isBaseStationConnected())
    {
      //如果没有连接基站，则等待连接成功
      codec_NBIoT->AT_CEREG();
      return;
    }
  if ("0" == socketID)
    {
      //如果socketID等于"0"则代表还未开启socket，开启socket
      codec_NBIoT->AT_NSOCR("9000");
      return;
    }
  logSaver->saveLog("register to server");
  codec_NBIoT->AT_NSOST(socketID,SERVERHOST,QString::number(SERVERPORT),makeRegisterData(msgId));
  ++msgId;
  timer_register.start(timer_interval_register);
}

void UDP_Test::slot_NEWSOCKETID(QString socketID)
{
  this->socketID = socketID;
}

void UDP_Test::slot_NEWSOCKETMSG(QString socketID, QString senderHost, ushort senderPort, int len, QByteArray data)
{
  (void)len;
  /*
  数据类型1字节，
  消息ID2字节，
  设备ID4字节，
  时间戳ID2字节
  载荷
  */
  if(SERVERHOST!=senderHost||SERVERPORT!=senderPort)
    {
      return;
    }
  logSaver->saveLog("new data:"+data.toHex());
  char* src=data.data();
  int size=data.size();
  int offset=0;
  char type = *(src+offset);
  offset+=sizeof (type);
  ushort msgId=*(ushort*)(src+offset);
  offset+=sizeof (msgId);
  int deviceId=*(int*)(src+offset);
  offset+=sizeof (deviceId);
  ushort timeStamp=*(ushort*)(src+offset);
  offset+=sizeof (timeStamp);
  QByteArray data1=QByteArray(src+offset,size-offset);
  logSaver->saveLog(QString("type:%1,msgId:%2,deviceId:%3,timeStamp:%4").arg((uint)type).arg(msgId).arg(deviceId).arg(timeStamp));

  if(TurnTable == type||Test == type)
    {
      codec_NBIoT->AT_NSOST(socketID, senderHost, QString::number(senderPort), data);
      return;
    }
  if (HeartBeat == type)
    {
      codec_NBIoT->AT_NSOST(socketID, senderHost, QString::number(senderPort), data);
      timer_register.start(timer_interval_register);
      return;
    }
  if (Register == type)
    {
      char registerType=data1.at(0);
      if(Registered==registerType)
        {
          connectionState = true;
          timer_register.start(timer_interval_register);
        }
      else if(Unregistered==registerType)
        {
          connectionState =false;
          slot_register();
        }
      else if(Reset==registerType)
        {
          connectionState=false;
          slot_register();
        }
    }
}

Codec_NBIoT *UDP_Test::getCodec_NBIoT()
{
  return codec_NBIoT;
}
void UDP_Test::setCodec_NBIoT(Codec_NBIoT *codec_NBIoT)
{
  if (Q_NULLPTR != this->codec_NBIoT)
    {
      disconnect(this->codec_NBIoT, 0, this, 0);
    }
  this->codec_NBIoT = codec_NBIoT;
  printInjection(this, codec_NBIoT);
  if (Q_NULLPTR != this->codec_NBIoT)
    {
      connect(codec_NBIoT, &Codec_NBIoT::signal_newSocketMsg, this, &UDP_Test::slot_NEWSOCKETMSG);
      connect(codec_NBIoT, &Codec_NBIoT::signal_newSocketId, this, &UDP_Test::slot_NEWSOCKETID);
    }
}

void UDP_Test::objectAutoInjection(QHash<QString, QObject *> *objMap)
{
  setCodec_NBIoT(getObjFromMap<Codec_NBIoT>(objMap));
}
